package com.itheima.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.*;
import com.itheima.service.ISetmealDishService;
import com.itheima.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ISetmealDishService setmealDishService;

    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto) {
//1、保存套餐数据到 setmeal表中 ： SetmealMapper
        setmealService.save(setmealDto);
//2、保存套餐菜品数据到 setmeal_dish表中： SetmealDishMapper
//2.1 获取当前套餐的菜品列表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
//2.2 循环遍历，获取每一个套餐菜品数据 setmealDish
        if (setmealDto != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                //2.3 获取套餐id，并set到setmealDish中
                setmealDish.setSetmealId(setmealDto.getId());
            }
            return R.success("套餐保存成功");
        } else {
            return R.error("套餐保存失败");
        }
    }

    @GetMapping("/page")
    public R pageQuery(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfor = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(!(StringUtils.isEmpty(name)), Setmeal::getName, name);
        setmealService.page(pageInfor, qw);
        List<Setmeal> records = pageInfor.getRecords();
        long total = pageInfor.getTotal();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return R.success(map);
    }

    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getStatus, 0);
        qw.in(Setmeal::getId, ids);
        return setmealService.remove(qw) ? R.success("套餐删除成功") : R.error("套餐正在售卖，删除失败");
    }

    @PostMapping("/status/{status}")
    public R updateByIds(@PathVariable Integer status, @RequestParam List<Long> ids) {
        boolean b = true;
        for (Long id : ids) {
            LambdaUpdateWrapper<Setmeal> uw = new LambdaUpdateWrapper();
            uw.eq(Setmeal::getId, id);
            uw.set(Setmeal::getStatus, status);
            b = setmealService.update(uw);
        }
        if (b) {
            return R.success("操作成功");
        }
        return R.error("操作失败");
    }

    @GetMapping("/{id}")
    public R selectDish(@PathVariable Long id) {
//1、查询菜品数据 select * from dish where id = ?
        Setmeal setmeal = setmealService.getById(id);
        //System.out.println(byId);
//2、查询菜品对应的口味数据 select * from setmealDish where dish_id = ?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(!(StringUtils.isEmpty(String.valueOf(id))), SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(qw);
        //System.out.println(one);
//返回DishDto对象，先new出该对象 DishDto dishDto = new DishDto();

        SetmealDto setmealDto = new SetmealDto();

// 把dish菜品数据 赋值/拷贝 给 dishDto对象 BeanUtils.copyProperties

        // 还需要将Dish dish 数据放入到 DishDto dishDto 里面
        BeanUtils.copyProperties(setmeal, setmealDto);

// 设置dishDto的口味数据 dishDto.setFlavors(xxx)
        setmealDto.setSetmealDishes(setmealDishes);
// 返回数据 ;
        return R.success(setmealDto);
    }

    @PutMapping
    public R updateSetmeal(@RequestBody SetmealDto setmealDto) {


// 1、修改菜品dish表的数据
        boolean update = setmealService.updateById(setmealDto);

        Long setmealId = setmealDto.getId();

//取出菜品id Long dishId = dishDto.getId();
// 2、修改菜品的口味数据 如何完成？？
// 2.1 先删除原有的口味数据: delete from dish_flavor where dish_id = ?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(qw);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
// 2.2 添加菜品口味数据 List<DishFlavor> flavors = dishDto.getFlavors();
            for (SetmealDish setmealDishe : setmealDishes) {
                //添加当前提交过来的口味数据---dish_flavor表的insert操作
                setmealDishe.setSetmealId(setmealId);
                setmealDishService.save(setmealDishe);
            }
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }

    }

    @GetMapping("/list")
    public R<List<SetmealDto>> querylist(Long categoryId) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId);
        List<Setmeal> list = setmealService.list(qw);
        List<SetmealDto> setmealDishList = new ArrayList<>();
        for (Setmeal setmeal : list) {

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            LambdaUpdateWrapper<SetmealDish> qw1 = new LambdaUpdateWrapper<>();
            qw1.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> setmealDishes = setmealDishService.list(qw1);
            //3.将菜品的口味列表 set给 dishDto
            setmealDto.setSetmealDishes(setmealDishes);
            //4.将dishDto放入到 dishDtos 集合中
            setmealDishList.add(setmealDto);
        }
        return list == null ? R.error("查询失败") : R.success(setmealDishList);
    }
}

